package cn.cque.controller;

import cn.cque.common.BaseController;
import cn.cque.entity.Cart;
import cn.cque.entity.Order;
import cn.cque.entity.User;
import cn.cque.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: XWC
 * @ClassName: OrderController
 * @Date: 2020/10/5
 * @Time: 14:39
 * @Description:
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/createOrder")
    public String createOrder() {
        //获取信息
        User user = (User) request.getSession().getAttribute("loginUser");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        //创建订单
        Order orders = orderService.createOrder(user, cart);

        //删除购物车
        request.getSession().removeAttribute("cart");
        //将数据存入request域
        request.setAttribute("order", orders);

        return "/WEB-INF/page/order-info";
    }

    @RequestMapping("/updateOrder")
    public String updateOrder(Order order) {
        /*//获取信息
        String id = request.getParameter("id");
        String address = request.getParameter("address");
        String name = request.getParameter("name");
        String telephone = request.getParameter("telephone");*/

        /*Order order=new Order();
        order.setId(id);
        order.setAddress(address);
        order.setName(name);
        order.setTelephone(telephone);*/
        //更新订单
        orderService.updateOrder(order);

        //重定向到订单列表
        return "redirect:/order/list";
    }

    @RequestMapping("/list")
    public String orderList() {
        //在session中获取用户
        User loginUser = (User) request.getSession().getAttribute("loginUser");

        //查询订单列表
        List<Order> orderList = orderService.findAllByUid(loginUser.getId());
        //存储数据
        request.setAttribute("orderList", orderList);
        //跳转页面
        return "/WEB-INF/page/order-list";
    }
}